package com.ydm.fire_fighting_web.modules.ucenter.web;


import com.ydm.fire_fighting_web.entity.ucenter.Result;
import com.ydm.fire_fighting_web.entity.ucenter.UserInfo;
import com.ydm.fire_fighting_web.modules.ucenter.api.UserControllerApi;
import com.ydm.fire_fighting_web.modules.ucenter.service.UserService;
import com.ydm.fire_fighting_web.servlet.BaseServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController extends BaseServlet implements UserControllerApi {

    @Autowired
    private UserService userService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;
    /**
     * 注册
     * @param userInfo
     * @return
     */
    @Override
    @PostMapping("/regist")
    public Result regist(UserInfo userInfo){
        //先判断验证码是否正确
        String check = request.getParameter("check");
        HttpSession session = request.getSession();
        String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER");
        //移除验证码,
        session.removeAttribute("CHECKCODE_SERVER");
        if(checkcode_server==null || !checkcode_server.equalsIgnoreCase(check)){
            //验证码错误
            Result result = new Result();
            result.setSuccess(false);
            result.setDetail(null);
            result.setMsg("验证码错误");
            return  result;
        }
        userInfo.setState(0);//默认设置为正常
        return userService.regist(userInfo);
    }

    /**
     * 登录
     * @param userInfo 参数封装
     * @return Result
     */
    @Override
    @PostMapping("/login")
    public Result login(UserInfo userInfo){
        //先判断验证码是否正确
        String check = request.getParameter("check");
        HttpSession session = request.getSession();
        String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER");
        //移除验证码,
        session.removeAttribute("CHECKCODE_SERVER");
        if(checkcode_server==null || !checkcode_server.equalsIgnoreCase(check)){
            //验证码错误
            Result result = new Result();
            result.setSuccess(false);
            result.setDetail(null);
            result.setMsg("验证码错误");
            return  result;
        }
        return userService.login(userInfo);
    }
    /**
     * 退出用户信息模块
     * @throws ServletException
     * @throws IOException
     */
    @PostMapping("/exit")
    public void exit(){
        HttpSession session = request.getSession();
        //销毁session
        session.invalidate();
        //重定向到登录页面
        try {
            response.sendRedirect(request.getContextPath()+"/login.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

